<template>
  <div class="user-list-container">
    <el-card>
      <!-- <template #header>
        <div class="card-header">
          <el-button type="primary" :loading="isRefresh" @click="handleRefresh">刷新</el-button>
        </div>
      </template> -->
      
      <!-- 搜索区域 -->
      <div class="search-container">
        <el-form :model="searchForm" inline>
          <el-form-item label="卡id">
            <el-input
              v-model="searchForm.CardId"
              placeholder="请输入卡id"
              clearable
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item label="完整卡号">
            <el-input
              v-model="searchForm.CompleteCardNumber"
              placeholder="请输入完整卡号"
              clearable
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item label="卡号后四位">
            <el-input
              v-model="searchForm.LastFourCardNumber"
              placeholder="请输入卡号后四位"
              clearable
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item label="商户">
            <el-select
              v-model="searchForm.MerchantId"
              placeholder="请选择商户"
              clearable
              style="width: 200px"
            >
              <el-option
                v-for="item in merchantIdOptions"
                :key="item.Id"
                :label="item.Name"
                :value="item.Id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">搜索</el-button>
            <el-button @click="handleReset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      
      <!-- 商户表格 -->
      <el-table 
        :data="list" 
        v-loading="loading"
        style="width: 100%"
        :default-sort="{ prop: 'createTime', order: 'descending' }"
        empty-text="暂无数据"
      >
        <el-table-column prop="CardId" label="用户卡id(uuid)" show-overflow-tooltip />
        <el-table-column prop="OrderId" label="订单id(uuid)" show-overflow-tooltip>
          <template #default="scope">
            <span>{{ scope.row.OrderId?.String }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="Amount" label="金额" width="150"/>
        <el-table-column prop="OrderStatus" label="订单状态" width="150" />
        <el-table-column prop="Merchant" label="商户名称" show-overflow-tooltip>
          <template #default="scope">
            <span>{{ scope.row.Merchant?.Name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="UpstreamPlatform" label="上游平台名称">
          <template #default="scope">
            <span>{{ scope.row.UpstreamPlatform?.Name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="Card" label="卡号" width="150" show-overflow-tooltip>
          <template #default="scope">
            <span>{{ scope.row.Card?.CardNumber || scope.row.Card?.CompleteCardNumber }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="CreatedAtString" label="创建时间" sortable show-overflow-tooltip />
        <!-- <el-table-column label="操作" width="200" fixed="right">
          <template #default="scope">
            <el-button type="text" size="small" @click="handleEdit(scope.row)">更新</el-button>
          </template>
        </el-table-column> -->
      </el-table>
      
      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 30, 50]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, computed } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { cardRechargeList } from '@/api/card'
import { merchantList } from '@/api/selectList'

// 响应式数据
const loading = ref(false)
const list = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const merchantIdOptions = ref([])
// 搜索表单
const searchForm = reactive({
  LastFourCardNumber: '',
  CompleteCardNumber: '',
  CardId: '',
  MerchantId: '',
})

// 获取列表
const getList = async () => {
  loading.value = true
  try {
    const res = await cardRechargeList({
      ...searchForm,
      Page: currentPage.value,
      PageSize: pageSize.value,
    })
    if (res.code === 200) {
      list.value = res.data?.List || []
      total.value = res.data?.Total
    } else {
      ElMessage.error('获取商户列表失败')
    }
  } catch (error) {
    // ElMessage.error('获取商户列表失败')
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  currentPage.value = 1
  getList()
}

// 重置搜索
const handleReset = () => {
  Object.assign(searchForm, {    
    LastFourCardNumber: '',
    CompleteCardNumber: '',
    CardId: '',
    MerchantId: '',
  })
  currentPage.value = 1
  getList()
}

// 分页大小改变
const handleSizeChange = (size) => {
  pageSize.value = size
  getList()
}

// 当前页改变
const handleCurrentChange = (page) => {
  currentPage.value = page
  getList()
}

const getMerchantIdOptions = async () => {
  try {
    const res = await merchantList()
    if (res.code === 200) {
      merchantIdOptions.value = res.data || []
    }
  } catch (error) {
    console.error(error)
  }
}
// 生命周期
onMounted(() => {
  getList()
  getMerchantIdOptions()
})
</script>

<style scoped>
.user-list-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

.search-container {
  margin-bottom: 20px;
  padding: 20px;
  background-color: #f8f9fa;
  border-radius: 4px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

:deep(.el-table .cell) {
  white-space: nowrap;
}

:deep(.el-form--inline .el-form-item) {
  margin-right: 20px;
  margin-bottom: 10px;
}
:deep(.el-table__header-wrapper .cell) {
  white-space: normal;
  word-break: break-all;
  line-height: 1.5;
}

/* 让表格滚动条始终显示 */
::v-deep(.el-table__body-wrapper .el-scrollbar__bar) {
  display: block!important;
}
:deep(.el-scrollbar__thumb) {
  background-color: #909399;
  opacity: 1;
}
</style>